﻿using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SocialFront.Core.Repository.EF;
using SocialFront.Core.Domain.Concrete;

namespace SocialFront.Tests
{   
    [TestClass]
    public class FooRepositoryTests
    {
        [TestMethod]
        public void SiblingsObjectContextWorking()
        {
            BuildDb();

            var uow = new EFUnitOfWork();
            //uow.DataContext.Configuration.ProxyCreationEnabled = true;
            var oCtx = ((IObjectContextAdapter)uow.DataContext).ObjectContext;
            //oCtx.ContextOptions.ProxyCreationEnabled = true;

            var fooSet = oCtx.CreateObjectSet<Foo>();
            fooSet.MergeOption = MergeOption.OverwriteChanges;
            var foo = fooSet.FirstOrDefault(x => x.Id == 3);
       
            var fooSibling1 = new FooSibling() { SiblingPropFoo = "Foo", SiblingPropFoo2 = "You" };
            var fooSibling2 = new FooSibling() { SiblingPropFoo = "Foo", SiblingPropFoo2 = "You" };
            foo.Siblings.Add(fooSibling1);
            foo.Siblings.Add(fooSibling2);

            oCtx.SaveChanges();
            oCtx.Dispose();

            Assert.IsTrue(foo.Siblings.Count() == 2);
        }

        private void BuildDb()
        {
            System.Data.Entity.Database.SetInitializer(new SocialFrontInitializer());
            var ctx = new SocialFrontDataContext("SocialFrontTesting");
            ctx.Database.Delete();
            ctx.Database.Create();
            ctx.Database.Initialize(true);
        }
    }
}
